import 'package:craze/models/index.dart';
import 'package:hive_flutter/hive_flutter.dart';

class HiveService {
  static Future<void> init() async {
    // 初始化Hive
    await Hive.initFlutter();

    // 注册适配器
    Hive.registerAdapter(UserModelAdapter());
    Hive.registerAdapter(QuestionModelAdapter());
    Hive.registerAdapter(AnswerModelAdapter());
    Hive.registerAdapter(VideoModelAdapter());
    Hive.registerAdapter(TeamModelAdapter());
    Hive.registerAdapter(SharePostModelAdapter());
    Hive.registerAdapter(ChatModelAdapter());
    Hive.registerAdapter(MessageModelAdapter());
    Hive.registerAdapter(ScanResultModelAdapter());
    Hive.registerAdapter(MarineProtectionModelAdapter());
    Hive.registerAdapter(UserSubmissionModelAdapter());
    Hive.registerAdapter(EmojiModelAdapter());
    Hive.registerAdapter(CollectionModelAdapter());
    Hive.registerAdapter(ShareCommentModelAdapter());

    // 打开盒子
    await Hive.openBox<UserModel>('users');
    await Hive.openBox<QuestionModel>('questions');
    await Hive.openBox<AnswerModel>('answers');
    await Hive.openBox<VideoModel>('videos');
    await Hive.openBox<TeamModel>('teams');
    await Hive.openBox<SharePostModel>('shares');
    await Hive.openBox<ChatModel>('chats');
    await Hive.openBox<MessageModel>('messages');
    await Hive.openBox<ScanResultModel>('scan_results');
    await Hive.openBox<MarineProtectionModel>('marine_protections');
    await Hive.openBox<UserSubmissionModel>('user_submissions');
    await Hive.openBox<EmojiModel>('emojis');
    await Hive.openBox<CollectionModel>('collections');
    await Hive.openBox<ShareCommentModel>('share_comments');
  }

  // 重置所有数据（开发测试用）
  static Future<void> resetAllData() async {
    // 清空所有盒子中的数据
    await Hive.box<UserModel>('users').clear();
    await Hive.box<QuestionModel>('questions').clear();
    await Hive.box<AnswerModel>('answers').clear();
    await Hive.box<VideoModel>('videos').clear();
    await Hive.box<TeamModel>('teams').clear();
    await Hive.box<SharePostModel>('shares').clear();
    await Hive.box<ChatModel>('chats').clear();
    await Hive.box<MessageModel>('messages').clear();
    await Hive.box<ScanResultModel>('scan_results').clear();
    await Hive.box<MarineProtectionModel>('marine_protections').clear();
    await Hive.box<UserSubmissionModel>('user_submissions').clear();
    await Hive.box<EmojiModel>('emojis').clear();
    await Hive.box<CollectionModel>('collections').clear();
  }
}
